package com.shyt.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.shyt.dao.SysRoleVo;
import com.shyt.mapper.SysRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class SysRoleServiceImpl {

    @Autowired
    SysRoleMapper sysRoleMapper;

    public List<SysRoleVo> getAllRoles() {
        return sysRoleMapper.getAllRoles();
    }

    public List<SysRoleVo> getRoleByRoleName(SysRoleVo sysRoleVo) {
        return sysRoleMapper.getRoleByRoleName(sysRoleVo);
    }

    public void addRole(SysRoleVo sysRoleVo) {
        sysRoleMapper.addRole(sysRoleVo);
    }

    public SysRoleVo selectRoleDeatil(SysRoleVo sysRoleVo) {
        QueryWrapper<SysRoleVo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",sysRoleVo.getId());
        queryWrapper.eq("role_code",sysRoleVo.getRoleCode());
        return sysRoleMapper.selectOne(queryWrapper);
    }

    public void editRole(SysRoleVo sysRoleVo) {
        UpdateWrapper<SysRoleVo> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("role_name",sysRoleVo.getRoleName());
        updateWrapper.set("remark",sysRoleVo.getRemark());
        updateWrapper.set("update_time",
                new SimpleDateFormat("yyyy-MM-dd HH:MM:ss").format(new Date()));
        updateWrapper.eq("id",sysRoleVo.getId());
        updateWrapper.eq("role_code",sysRoleVo.getRoleCode());

        sysRoleMapper.update(sysRoleVo,updateWrapper);

    }

    public void deleteRole(SysRoleVo sysRoleVo) {
        sysRoleMapper.deleteById(sysRoleVo.getId());
        sysRoleMapper.deleteUserRole(sysRoleVo);
        sysRoleMapper.deleteRolePermissiom(sysRoleVo);
    }
}
